﻿package com.xuanque.mahjongDice.control 
{
	import com.xuanque.mahjongDice.core.*;
	import com.xuanque.mahjongDice.display.*;
	import com.xuanque.mahjongDice.effects.TweenTogetherItems;
	import com.xuanque.mahjongDice.Main;
	import com.xuanque.mahjongDice.media.SoundControl;
	import com.xuanque.mahjongDice.ui.CountdownClock;
	import com.xuanque.mahjongDice.ui.QcontextMenu;
	import utils.HashMap;
	import com.xuanque.mahjongDice.util.LoaderImg;
	import com.xuanque.mahjongDice.util.SignalAction;
	import com.xuanque.mahjongDice.util.ToolTip;
	import com.xuanque.mahjongDice.util.Tools;
	
	import datas.Player;
	import datas.Players;
	import datas.User;
	import datas.MajDice;
	
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.utils.Timer;
	
	import utils.transitions.*;
	import flash.system.ApplicationDomain;
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class GameAction 
	{
		static private var Instance:GameAction;
		
		private var gameAreaControl:*;
		private var infoPanel:InfoPanel;
		public var _stageMain:*;
		
		public var allInstance_array:Array;//4个元件容器 实例名
		public var hashMap:HashMap = new HashMap();; // 发牌的时候存储  pos/dice
		
		private var userPos:int; //本人位置
		
		public var enter_array:Array; //已经存在多少个用户
		public var enter_length:uint;
		
		private var user_Instance:*;
		
		private var users_timer:CountdownClock;
		
		private var isLoadSnd:Boolean;//首次加载音乐
		private var isOpenSnd:Boolean;//音乐
		private var userTrusteeship:Boolean;//本人 托管
		
		private var num_Instance:uint = 0; //0,1,2,3  0是本人
		private var tweenTogetherItems:TweenTogetherItems;
		private var newX_array:Array = [ 280, -180, -190, 190];
		private var newY_array:Array = [ -130, -150, 80, 120];
		
		private var TimerX_array = [10 ,120, 10, -155 ];
		private var TimerY_array = [65,-30, -130, -30 ];
		
		private var resulttSiceX:Number;
		private var resulttSiceY:Number;
		
		private var gameFirstNum:uint;//公骰
		private var sicesPos:uint;//本人用户的 骰子 位置
		private var selectNum:uint;//手动选择打出的骰
		
		private var isrest:Boolean;
		private var isFirstPriority:Boolean;
		
		private var boutTime:int = 0;//每回合时间
		private var readyTime:int = 0;//准备时间
		private var shakeTime:int;//摇骰时间
		
		private var _timer:Timer;
		private var timer_mc:*;
		private var timer_count:uint;
		
		public function GameAction(gameAreaControl:*,infoPanel:InfoPanel,_this:*) 
		{
			
			trace("-------------------------000000000000000000000000");
			Instance = this;
			allInstance_array = [ "player0", "player1",  "player2", "player3"];
			
			this.gameAreaControl = gameAreaControl;
			this.infoPanel = infoPanel;
			this._stageMain = _this;
			
			this.user_Instance = gameAreaControl.player0;
			
			this.shakeTime = MajDice.getInstance().getShakeTime();
			this.boutTime = MajDice.getInstance().getBoutTime();
			this.readyTime = MajDice.getInstance().getReadyTime() == 0?50:MajDice.getInstance().getReadyTime();
			
			this.initGame(gameAreaControl); //进入 游戏 初始化，隐藏用户元件 
			
			
			if (this.isLoadSnd == false) {
				
				SoundControl.getInstance().loadMusic("music/majDice/bgmusic01.mp3");
				this.isLoadSnd = true;
				
				this.gameAreaControl.control_btns.visible = true;
				this.gameAreaControl.control_btns.sound_btn.addEventListener(MouseEvent.CLICK, soundHandler);//背景音乐
				this.gameAreaControl.control_btns.full_btn.addEventListener(MouseEvent.CLICK, maxWinHandler );
			}
			
		}
		
		
		/*
		 * 单件类的实例 
		*/
		public static function getInstance():GameAction
        {
            return Instance;
        }
		
		
		public function getConnect():void {
			
		}
		
		
		/*
		 * 返回本人固定位置 
		*/
		public function getUserPos():int {
			
			return this.userPos;
		}
		
		
		
		
		/*
		 * 确定庄家 ( 发骰 后第一个摇骰 )
		*/
		public function host(username:String):void{
			
			
			var user:* = this.getThisUser(username);
			
			if ( user != null ) user.host_mc.visible = true;
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		/* 示例子
		
		var all_players:Array = [ 
									[ 1 ,[1, 1, 1, 2, 2, 3, 6]],
									[ 2 , [2, 2, 2, 2, 2, 3, 6] ],
									[ 0 , [4, 4, 4, 2, 2, 3, 6] ],
									[ 3 , [3, 3, 3, 2, 2, 3, 6] ]
									
								];
		*/
		
		
		/*
		 * 首发骰给各个玩家 ( 所有 骰 、位置 )
		*/ 
		public function gameStart( _dices:Array ):void {	
			
			trace("首发骰给各个玩家 ( 所有 骰 、位置 ) ==== " + _dices);
			
			var pos:int;
			var dices:Array;
			for (var i = 0; i < _dices.length; i++) {
				
				pos = _dices[i][0];
				dices = _dices[i][1];
				
				dices = dices.sort(); //排序
				
				this.hashMap.put( pos , dices );
			}
				
			
			
		}
		
		
		
		
		
		/*
		 * 开始 发骰
		*/
		public function typeOutControl(isFaPaiMove = true):void {
			
			
			
			this.displayUserChildren(false); //隐藏 准备
			this.displayUserPan(true);//显示 本人的一些控制按钮
			this.displayOtherControl(true);//显示其它人
			
			
			
			if (isFaPaiMove == true) {
				
				this.yaoSaiMove();
			}
			
			
		}
		
		
		/*
		 * 打公骰	
		*/
		public function getSystemPai(num:uint):void {
			
			//开始发首张系统牌,4家都有机会湖牌
			this.gameFirstNum = num; //系统摇出一张牌
			this.gameAreaControl.resulttSice.visible = true;
			var control_mc = this.gameAreaControl.gameFirstControl_mc;
			control_mc.addFrameScript(0, function(){control_mc.stop()}, control_mc.totalFrames - 1, this.addFrameScriptStart); 
			
			control_mc.visible = true;
			control_mc.gotoAndPlay(2);
			
		}
		private function addFrameScriptStart():void {
			
			this.displayResulttSice( this.gameFirstNum );
		}
		
		
		
		
		
		/*
		 * 打骰接力 ( 动画播放 2秒  )
		*/
		public function priority(username:String):void {
			
			if (this.isFirstPriority == false) {
				
				this.isFirstPriority = true;
				this.gameAreaControl.resulttSice.gotoAndStop(1);
			}
			
			
			trace("打骰接力------user=" + username);
		    var user:* = this.getThisUser(username);
		  	trace("打骰接力------user=" + user);
			
		    if ( user != null ) {
				
				if (user.name == "player0") {
					
					user.shak_mc.visible = true;//摇骰按钮
					user.shak_mc.mouseEnabled = true;
					user.shak_mc.addEventListener(MouseEvent.CLICK, yaoSaiHandler);
					
				}else {
					//userYaoSai(username:String) 放到这个方法中
					//user.turnMe_mc.gotoAndPlay(2);//摇骰 动画播放
				}
				
				
			}
			
			trace("时钟位置 user.name==== " + user.name );
			
			for (var i = 0; i < 4; i++) {
				
				if ( user.name == this.allInstance_array[i] ) {
					
					this.gameAreaControl.timer_mc.visible = true; // 显示 倒计时(所有用户统一用这个)
					this.users_timer = this.gameAreaControl.timer_mc;
					this.users_timer.x = this.TimerX_array[i];
					this.users_timer.y = this.TimerY_array[i];
					trace("摇骰接力时间 ====" + this.shakeTime);
					this.users_timer.setTimer( 1000,this.shakeTime );
					this.users_timer.startClock( "priorityTimerOut" );
					
					trace("时钟位置 this.allInstance_array[i] " + this.allInstance_array[i] );
				}
				
			}
			
			
		}
		private function yaoSaiHandler(event:MouseEvent):void {
			
			this.gameAreaControl.control_btns.trusteeship_btn.visible = false;//隐藏 托管等按钮 ( 本人摇完才显示，摇时不显示)
			
			//this.gameAreaControl.timer_mc.visible = false;
			this.users_timer.visible = false;
			
			var turnMe_btn:SimpleButton = event.target as SimpleButton;
			turnMe_btn.removeEventListener(MouseEvent.CLICK, yaoSaiHandler);
			turnMe_btn.visible = false; //摇骰完就隐藏
			
			SendPackage.getInstance().shaking() // 发包 摇骰包
			
			//userYaoSai("lbynet 1"); 测试用
		}
		
		/*
		 *  摇骰 动画播放
		*/
		public function userYaoSai(username:String):void {
			
			var user:* = this.getThisUser(username);
		  	
		    if ( user != null ) user.turnMe_mc.gotoAndPlay(2);//摇骰 动画播放
			
		}
		
		
		private var yaoSaiName:String;
		private var yaoSaipai:int;
		/*
		 * 取牌 摇骰 后显示牌
		*/
		public function getPaiYaoSai(username:String, pai:int):void {
			
			this.yaoSaiName = username;
			this.yaoSaipai = pai;
			
			var user:* = this.getThisUser(username);
		  	if ( user == null ) {  
				
				trace("****************** user 不存在  *****************");
				return;
			}
			
			if ( username == User.getInstance().getLoginName() ) {
				
				this.user_Instance.shak_mc.mouseEnabled = false;
				this.user_Instance.shak_mc.visible = false; //摇骰完就隐藏
				
				/*
				if (this.userTrusteeship == true) {
					
					trace("+++++++++++++++++  托管时不摇  ++++++++++++++++");
					this.userYaoSaiComplete()//托管时不摇
					
					return;//返回
				}
				*/
			}
			
		    if (user.trustee_mc.currentFrame == 2) {
				
				trace("+++++++++++++++++  托管时不摇  ++++++++++++++++");
				this.userYaoSaiComplete()//托管时不摇
				
				return;//返回
				
			}
			
			
			if ( username == User.getInstance().getLoginName() ) {
				
				this.gameAreaControl.control_btns.trusteeship_btn.visible = false;//隐藏 托管等按钮 ( 本人摇完才显示，摇时不显示)
			}
			
			user.turnMe_mc.addFrameScript(0, function(){user.turnMe_mc.stop()} , user.turnMe_mc.totalFrames - 1, this.userYaoSaiComplete); 
			user.turnMe_mc.gotoAndPlay(2);//摇骰 动画播放
			
			
		}
		
		
		private function userYaoSaiComplete():void {
			
			if (yaoSaiName != null && yaoSaipai != 0) {
				
				
				trace( "+++++++++++++++++  userYaoSaiComplete  ++++++++++++++++" + yaoSaiName + "  " + yaoSaipai );
				displayGetPai( this.yaoSaiName,this.yaoSaipai );
			}
			
			
		}
		
		/*
		 * 通知摇出的骰 ( 显示 取得的 牌 ,然后 进行一些本人用户的控制 )
		*/
		public function displayGetPai(username:String, pai:int):void {
			
			var pos:int = Players.getInstance().getPlayerInfo(username).getPosition();
			var dices:Array = this.hashMap.getValue(pos);
			//将摇出来的骰保存到该玩家Array
			dices.push(pai);
			trace(username+"通知摇出的骰"+this.hashMap.getValue(pos));
            var user:* = this.getThisUser(username);
			
			
			trace("通知摇出的骰username ====" + username );
			trace( "通知摇出的骰 ==== " + pai );
			trace("该用户所有的牌 ==== " + dices );
			
			GameTip.writeOffInstance();// 注销一下
			var isHuPai:Boolean = GameTip.getInstance().setJoinPaiArray( dices );
			trace("提示是否 胡牌 ==== " + isHuPai);
			
			
            if (user != null) {
				
				this.getPlayersPai(user, pai);
				
				
				
				if (isHuPai) {
					
					trace( "胡牌用户 ==== " + username );
					GameAction.getInstance().huPaiResults( username );//提示是否 胡牌
				}else {
					
					this.users_timer.visible = true;
					trace("打骰接力时间 ====" + this.boutTime);
					this.users_timer.setTimer( 1000,this.boutTime );
					this.users_timer.startClock( "priorityTimerOut" );
				}
				
				
				
			}
			
			
			
			
			if ( username == User.getInstance().getLoginName() && isHuPai != true ) {	
				
				this.displayUserControl(true);//显示 本人的一些控制按钮
				
				sicesControlHandler();//统一 侦听每一个 骰子	
				this.user_Instance.getOut_btn.addEventListener(MouseEvent.CLICK, getOutItemHandler);
			    this.user_Instance.tip_btn.addEventListener(MouseEvent.CLICK, getOutTipHandler);
			}
			
			
			this.yaoSaiName = null;
			this.yaoSaipai = 0;
			
			
			this.gameAreaControl.control_btns.trusteeship_btn.visible = true;//显示 托管等按钮 ( 本人摇完才显示，摇时不显示)
			
			
		}
		
		/*
		 * 统一 侦听和移除侦听 每一个 骰子
		*/
		private function sicesControlHandler( isListener = true ):void {
			
			for (var i = 1; i <=8;i++ ) {
				var pai_mc:MovieClip = this.user_Instance.paiArray_mc["sice0" + i];
				
				if (isListener == true) {
					
					pai_mc._btn.addEventListener(MouseEvent.CLICK, this.selectItemHandler);
				}else {
					
					pai_mc._btn.removeEventListener(MouseEvent.CLICK, this.selectItemHandler);
				}
				
				
			}
			
			
		}
		
		/* 出骰 按钮 的侦听 */
		private function getOutItemHandler(event:MouseEvent):void {
			
			if (this.selectNum != 0) {
				
				
				selectItemTween(true);
				
				this.sicesControlHandler( false ); //统一 移除 每一个 骰子 侦听
				this.user_Instance.tip_btn.removeEventListener(MouseEvent.CLICK, getOutTipHandler);
				
				SendPackage.getInstance().getOutPai( this.selectNum );//发包  玩家 出骰 点
				
				this.selectNum = 0; //设为0
				this.sicesPos = 0; //设为0
				
				displayUserControl(false);
			}
			
		}
		
		/* 提示出骰 按钮 的侦听 */
		private function getOutTipHandler(event:MouseEvent):void {
			
			var user_array:Array = this.hashMap.getValue( this.userPos );
			
			var dice:MahJongDice = new MahJongDice();
			dice.setJoinPaiArray(user_array);
			
			this.selectNum = dice.getTipItem();
			this.sicesPos = this.searchPaiPosition( this.selectNum );
			
			trace(" this.selectNum = " + this.selectNum);
			trace(" this.sicesPos = " + this.sicesPos);
			
			selectItemTween();
			
			
		}
		
		
		/*
		 * 搜索 取牌 后的所有牌中被选择的  牌 ,返回其 牌 的位置
		*/
		private function searchPaiPosition(_uselessNum:Number):uint {
			
			var _array:Array = this.hashMap.getValue( this.userPos );
			
			for (var i:uint = 1; i <= 8; i++ ) {
				
				var searchNum:Number = _array[i - 1];
				if (searchNum == _uselessNum) return i; //第几个
				
			}
			
			return NaN;
		}
		
		/*
		 * 所选择按钮对应的骰子点数
		*/
		private function selectItemHandler(event:MouseEvent = null):void {
			
			if (this.enter_length == 7) {
				trace("所选择按钮对应的骰子点数 不能被选择");
				return;
			}
			var _btn:SimpleButton = event.target as SimpleButton;
			var item_mc:MovieClip = _btn.parent as MovieClip;
			
			var itemName:String = item_mc.name;
			
			this.sicesPos = uint(itemName.slice(5, 6));
			
			var user_array:Array = this.hashMap.getValue( this.userPos );
			
			this.selectNum = user_array[ this.sicesPos - 1 ];
			
			trace( "++++++所选择按钮对应的位置+++++++ = " + sicesPos );
			trace("+++++++++++++所选择按钮对应的骰子点数 =" +  this.selectNum );
			
			selectItemTween();
			
		}
		
		
		
		
		/*
		 * 出骰通知 ( 显示 发出去的 牌 )
		*/
		public function displayOutPai(username:String, pai:int):void{
			
			this.gameAreaControl.resulttSice.gotoAndStop(1);
			
			var pos:int = Players.getInstance().getPlayerInfo(username).getPosition();
			var dices:Array = this.hashMap.getValue(pos);
			trace(username+"出骰之前="+dices);
			//从hashMap删除该玩家打出去的骰
			var position:int = dices.indexOf(pai);
			dices.splice(position,1);
			trace(username + "出骰之后=" + dices);
			
			if (username == User.getInstance().getLoginName() ) displayUserControl(false);
			
			var user:* = this.getThisUser(username);
			if (user) {
				this.changedUserPai( pos , dices);
				
				this.displayResulttSice( pai, this.getThisUser(username));
				
				this.users_timer.stopClock();
				this.users_timer.visible = false;
			}
			
			
		}
		
		
		/*
		 * 胡牌验证
		*/
		public function huPaiTip(username:String,status:int = 0):void {
			
			
			var user:* = this.getThisUser(username);
			user.huPai_mc.gotoAndPlay(2);
			
			if (user == this.user_Instance) {
				
				this.displayUserControl(false);
				
				user.huPai_btn.visible = false;
				user.huPaiTip_mc.visible = false;
				user.huPaiTip_mc.gotoAndStop(1);
				//user.huPai_btn.removeEventListener(MouseEvent.CLICK, huPaiHandler);
			}
			
			//this.gameAreaControl.resulttSice.visible = false;
			this.users_timer.stopClock();
			this.users_timer.visible = false;
			
			
			this.userTrusteeship = false;
			this.isClickTrusteeship = false;
			this.gameAreaControl.control_btns.trusteeship_btn.gotoAndStop(1);
			this.gameAreaControl.control_btns.trusteeship_btn.visible = false;
			
			var allInstance:*;
			for (var i = 0; i < 4; i++) {
				
				allInstance = this.gameAreaControl[ allInstance_array[i] ];
				allInstance.trustee_mc.visible = false;//取消托管
				allInstance.trustee_mc.gotoAndStop(1);
			}
			
			
			
		}
		
		/*
		 * 胡牌提示  (胡牌 按钮 提示 需要发一个空包,只对本人)
		*/
		public function huPaiResults(username:String):void {
			
			trace("胡牌玩家username=====" + username);
			
			var user:* = this.getThisUser(username);
			
			if ( user != null && username == User.getInstance().getLoginName() ){
				
				user.huPai_btn.visible = true;
				
				user.huPaiTip_mc.visible = true;
				user.huPaiTip_mc.gotoAndPlay(2);
				user.huPai_btn.addEventListener(MouseEvent.CLICK, huPaiHandler);
				
			}
			
			
			
		}
		
		private function huPaiHandler(event:MouseEvent):void {
			
			var huPai_btn:* = event.target;
			huPai_btn.removeEventListener(MouseEvent.CLICK, huPaiHandler);
			huPai_btn.visible = false; //摇骰完就隐藏
			huPai_btn.parent.huPaiTip_mc.gotoAndStop(1);
			
			this.user_Instance.huPai_btn.removeEventListener(MouseEvent.CLICK, huPaiHandler);
			this.user_Instance.huPaiTip_mc.visible = false;
			this.user_Instance.huPaiTip_mc.gotoAndStop(1);
			
			
			
			
			trace("+++++++++++++++++胡牌玩家发包++++++++++++++++");
			
			SendPackage.getInstance().huPai() //发包
			
		}
		
		
		
		
		
		
		
		
		
		/*
		 * 设置玩家准备状态
		*/
		public function setUserReady( username:String ):void{
		    
			var user:* = this.getThisUser(username);
			
			if ( user != null ) {
				
				user.ready_mc.visible = true;
				user.ready_mc.gotoAndStop(2);
			}
			
		}
		
		/*
		 * 设置玩家 取消准备状态
		*/
		public function cancelUserReady( username:String ):void{
		    
			var user:* = this.getThisUser(username);
			
			if ( user != null ) {
				
				user.ready_mc.visible = true;
				user.ready_mc.gotoAndStop(3);
			}
			
		}
		
		
		/*
		 * 根据用户名取得Player 
		*/
		private function getThisPlayer(username:String):Player {
			
			for (var i = 0; i < this.enter_array.length; i++ ) {
				
				if ( this.enter_array[i].getLoginName() == username ) {
					
					return this.enter_array[i];
				}
				
			}
			
			return null;
			
		}
		
		/*
		 * 根据 用户名 转换相对位置 （4个对象中的一个） 
		*/
		public function getThisUser( username:String ):* {
			
			
			var testPlayer:* ;
			var relativePos:int;
			
			//trace(this.enter_length);
			
			if ( username == User.getInstance().getLoginName() ) {
				
				testPlayer = this.gameAreaControl[ allInstance_array[0] ]; //固定的位置
				return testPlayer;
				
			}else {
				
				
				for (var i = 0; i < this.enter_length; i++) {
					
					if ( username == this.enter_array[i].getLoginName() && username != User.getInstance().getLoginName()) {  //已经存在 用户名
						
						relativePos = Tools.getPositionByPos( this.userPos, this.enter_array[i].getPosition() ); // 转换 为 相对位置
						
						testPlayer = this.gameAreaControl[ allInstance_array[relativePos] ]; //固定的位置
						
						return testPlayer;
					}
					
					
				}
				
				
			}
			
			
				
			
			
			return null;
			
			
		}
		
		
		
		
		/*
		 * 根据 位置 转换相对位置 （4个对象中的一个） 
		*/
		public function getUserByPos( pos:int ):* {
			
			
			var testPlayer:* ;
			var relativePos:int;
			
			if ( pos == this.userPos ) {
				
				testPlayer = this.gameAreaControl[ allInstance_array[0] ]; //固定的位置
				return testPlayer;
				
			}else {
				
				for (var i = 0; i < this.enter_length; i++) {
					
					if ( pos == this.enter_array[i].getPosition() ) {  //已经存在 用户 的外部 位置
						
						relativePos = Tools.getPositionByPos( this.userPos, this.enter_array[i].getPosition() ); // 转换 为 相对位置
						
						testPlayer = this.gameAreaControl[ allInstance_array[relativePos] ]; //固定的位置
						
						return testPlayer;
					}
					
					
				}
				
			}
			
			
			return null;
			
			
		}
		
		
		
		
		/*
		 * 第一次进入游戏,初始化用户界面
		*/
		public function enterGame(players:Array):void {
			
			var field:TextField = Main.getInstance().bet_mc.bet_txt;
			field.autoSize = TextFieldAutoSize.LEFT;
			
			var format:TextFormat = new TextFormat();
			format.bold = true;
			field.defaultTextFormat = format;
			//Main.getInstance().bet_mc.visible = true;//隐藏底注
			//trace(" MajDice.getInstance().getAnte() ====" + MajDice.getInstance().getAnte());
			field.text = "" + MajDice.getInstance().getAnte();
			
			
			this.userPos = User.getInstance().getPosition(); //取得本人位置 ,固定的位置
			
			trace("第一次进入游戏 本人位置this.userPos ====== " + this.userPos);
			
			this.infoPanel.userInfo.bulidUserInfo( User.getInstance() ); //显示本人的信息
			
			
			
			this.enter_array = players;
			this.enter_length = players.length;
			
			var testPlayer:* ;
			var othersPos:int;
			
			for (var i = 0; i < this.enter_length; i++) {
				
				this.updateUserList(players[i]); //更新玩家列表
				
				if ( this.userPos == players[i].getPosition() ) { //本人
					
					testPlayer = this.gameAreaControl[allInstance_array[0]]; //固定的位置
					
				}else {
					
					othersPos = Tools.getPositionByPos( this.userPos, players[i].getPosition() ); //相对的位置
					testPlayer = this.gameAreaControl[allInstance_array[ othersPos ]]; 
					
				}
				
				
				if ( players[i].getRoomStatus() == 2 ) {
					
					GameAction.getInstance().setUserReady( players[i].getLoginName() );
				}
				
				
				if (testPlayer is MovieClip) {
					
					ImgAction.getInstance().enterUser(players[i]); //显示 头像 和 用户名
					
					
				}
				
				
			}
			
		}
		
		
		/*
		 * 某一用户加入游戏
		*/
		public function addGame(player:Player):void {
			
			this.updateUserList(player);//更新玩家列表  和 聊天窗口
			
			this.enter_array.push(player); //加入
			this.enter_length = this.enter_array.length;
			
			
			var testPlayer:* ;
			var othersPos:int;
			
			othersPos = Tools.getPositionByPos( this.userPos, player.getPosition() ); //相对的位置
			testPlayer = this.gameAreaControl[allInstance_array[ othersPos ]]; 
			
			if (testPlayer is MovieClip) ImgAction.getInstance().enterUser(player); //显示 头像 和 用户名
			
		}
		
		
		/*
		 * 离开游戏，更改界面 (包括本人和其它用户) ....
		*/
		public function leaveGame(player:Player):void {
			trace("玩家离开游戏:"+player.getLoginName());
			if ( User.getInstance().getLoginName() == player.getLoginName() ) {
				
				//SoundControl.getInstance().stopMusic();
				this.users_timer.stopClock();
				//if(GameResult.getInstance())
				//GameResult.getInstance().stopTimer();
				ApplicationDomain.currentDomain.getDefinition("loadswf.MainLoad").deleteGame();
				
			}else {
				
				this.updateUserList(player,true);//更新玩家列表
				
				var num:int = this.enter_array.indexOf(player);
				this.enter_array.splice(num,1); //删除
				this.enter_length = this.enter_array.length;
				
				
				var testPlayer:* ;
				var othersPos:int;
				
				othersPos = Tools.getPositionByPos( this.userPos, player.getPosition() ); //相对的位置
				testPlayer = this.gameAreaControl[allInstance_array[ othersPos ]]; 
				
				if (testPlayer is MovieClip) {
					
					testPlayer.ready_mc.gotoAndStop(3);
					ImgAction.getInstance().leaveUser(player); //显示 头像 和 用户名
					
				}
				
				
			}
			
			
			
			
		}
		
		
		
		/*
		 * 结算结果
		*/
		public function showResults(result_array:Array):void {
			
			_stageMain.showResults(result_array);
			
			this.sicesControlHandler(false); //统一 移除侦听 每一个 骰子
			this.user_Instance.getOut_btn.removeEventListener(MouseEvent.CLICK, getOutItemHandler);
			this.user_Instance.tip_btn.removeEventListener(MouseEvent.CLICK, getOutTipHandler);
			
			
		}
		
		/*
		 * 游戏结束，可以重新开始
		*/
		public function gameOver():void {
			
			_stageMain.gameOver();
		}
		
		
		
		/*
		 * 强行退出游戏,断线
		*/
		public function outOfGame(username:String ,isout:Boolean = true ):void{
			
			var user:* = this.getThisUser(username);
			var player:Player = this.getThisPlayer( username );
			
			if ( user != null ) {
				
				if(username != User.getInstance().getLoginName()) user.lost_mc.visible = isout;
				
				if (isout)	ImgAction.getInstance().leaveUser( player ); //隐藏用户图标
				
				
			}
			
		}
		
		
		
		/*
		 * 托管
		*/
		public function trusteeship(username:String, istrue:Boolean = true):void {
			
			var user:* = this.getThisUser(username);
			
			if ( user != null) {
				
				if (username == User.getInstance().getLoginName()) {
					
					//Main.getInstance().infoPanel.chatWindow.joinText( ("您" + (istrue==true?"已经":"取消了") +"托管。") ,  "" ,"onlyContent" );//更新 聊天窗口
					
					this.userTrusteeship = istrue; //是否是本人托管
				}
				
				user.trustee_mc.visible = istrue;
				user.trustee_mc.gotoAndStop( istrue?2:1 );
				
			}
			
		}
		
		
		
		
		/*
		 * 更新用户列表
		*/
		public function updateUserList(player:Player,isOut:Boolean = false):void{
			
			if (isOut) {
				
				this.infoPanel.playerList.removeUser(player);//用户离开，更新显示列表
				this.infoPanel.chatWindow.joinSystemText( "离开房间。", player.getNickName() );//更新 聊天窗口
				
				this.infoPanel.resultCard.removeUser(player);//更新结果列表
				
			}else {
				
				this.infoPanel.playerList.addPlayerUser(player);//显示列表中加入用户信息
				this.infoPanel.chatWindow.joinSystemText( "进入房间。",player.getNickName() );//更新 聊天窗口
				
				
				this.infoPanel.resultCard.addPlayerUser(player);//更新结果列表
				
			}
			
			
		}
		
		/*
		 * 更新游戏结果
		*/
		public function updateResultCard(result_array:Array):void {
			
			
			this.infoPanel.resultCard.updateUserResult(result_array);//更新结果列表
			
		}
		
		
		
		
		/*
		 * 接收聊天信息
		*/
		public function chating(username:String, content:String):void {
			
			var player:Player = this.getThisPlayer(username);
			this.infoPanel.chatWindow.joinText(content, player.getNickName());
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		public function startReadyClock():void {
			
			this.users_timer = this.gameAreaControl.timer_mc;
			this.users_timer.visible = true;
			this.users_timer.x = 10;
			this.users_timer.y = 65;
			this.users_timer.setTimer( 1000, this.readyTime );
			this.users_timer.startReady();
		}
		
		/*
		 * 进入 游戏 初始化，隐藏用户元件 
		*/
		public function initGame(core_mc:*):void {
			
			with (core_mc) {
				
				gameFirstControl_mc.visible = false;
				allSize_mc.visible = false;
				yaoSaiMove_mc.visible = false;
				resulttSice.gotoAndStop(1);
				resulttSice.visible = false;
				control_btns.trusteeship_btn.visible = false;
				//control_btns
			}
			
			startReadyClock();
			
			
			var player:MovieClip;
			
			for (var i = 0; i < 4; i++) {
				
				player = core_mc[ allInstance_array[i] ];
				
				//trace("**********************************" + i)
				
				if (i == 0) {
					
					player.paiArray_mc.visible = false;
					player.siceBg_mc.visible = false;
					
					player.shak_mc.visible = false;
					player.getOut_btn.visible = false;
					player.tip_btn.visible = false;
					
					player.huPaiTip_mc.gotoAndStop(1);
					player.huPaiTip_mc.mouseEnabled = false;
					player.huPaiTip_mc.visible = false;
					player.huPai_btn.visible = false;
					
					player.ready_mc.gotoAndStop(1);
					player.ready_mc.visible = true;
					player.ready_mc.addEventListener(MouseEvent.CLICK, readyHandler); //开始 按钮被单击
					
					
					
				}else {
					
					if (this.isrest == false) {
						player.ready_mc.gotoAndStop(3);
					}
					
					player.ready_mc.visible = true;
					
					player.lost_mc.visible = false;
					player.paiArray_mc.visible = false;
					
				}
				
				for (var j = 1; j <= 8; j++) {//只设置7个
					
					if (j == 8) {
						player.paiArray_mc["sice0" + j].gotoAndStop("isnull");
					}else {
						player.paiArray_mc["sice0" + j].visible = false;
					}
					
					player.paiArray_mc["sice0" + j].scaleX = 1;
					player.paiArray_mc["sice0" + j].scaleY = 1;
				}
				
				
				player.turnMe_mc.visible = false;
				player.host_mc.visible = false;
				player.huPai_mc.gotoAndStop(1);
				
				if (this.isrest == false) {
					
					player.player_txt.visible = false;
					player.photo_mc.visible = false;
					player.kuang_mc.visible = false;
					
					player.trustee_mc.visible = false;//取消托管
					player.trustee_mc.gotoAndStop(1);
				}
				
				
			}
			
			if (this.isrest == false) {
				
				core_mc.control_btns.visible = false; //隐藏 托管等按钮
			}
			
			
		}
		
		/*
		 * 单击 准备 按钮
		*/
		private function readyHandler(event:MouseEvent):void {
			
			this.users_timer.stopClock();//停止倒计时
			this.users_timer.visible = false;
			
			SoundControl.getInstance().loadSound("music/majDice/startBtn.mp3");
			
			if (this.user_Instance.currentFrame == 1) {
				
				this.user_Instance.ready_mc.removeEventListener(MouseEvent.CLICK, readyHandler);
			}
			
			this.user_Instance.ready_mc.gotoAndStop(2);
			this.user_Instance.ready_mc.mouseEnabled = false;
			
			
			SendPackage.getInstance().sendReady(); // 发包 准备 包
			
			
			
		}
		
		
		/*
		 * 超过准备时间没有准备--发包请求退出
		*/
		public function readyTimeOut(event:Event):void {
			//trace("超过准备时间没有准备--发包请求退出");
			this.users_timer.stopClock();
			SendPackage.getInstance().sendLeave();
		}
		
		/*
		 * 接力时间超时
		*/
		public function priorityTimerOut(event:Event):void {
			
			this.users_timer.stopClock();
			this.users_timer.visible = false;
			
			trace("************** 接力时间超时 ************** ");
		}
		public function OutServerTimerOut(event:Event):void {
			
			this.users_timer.stopClock();
			this.users_timer.visible = false;
			trace("************** 断线重连时间超时************** ");
		}
		
		
		/*
		 * 显示 和 隐藏 控制按钮
		*/
		private function displayUserControl( isDisplay = true ):void {
			
			this.user_Instance.getOut_btn.visible = isDisplay;
			this.user_Instance.tip_btn.visible = isDisplay;
			
			this.user_Instance.getOut_btn.mouseEnabled = isDisplay;
			this.user_Instance.tip_btn.mouseEnabled = isDisplay;
			
			//this.user_Instance.huPai_btn.visible = isDisplay;
			//this.user_Instance.shak_mc.visible = isDisplay;//摇骰按钮
		}
		
		/*
		 * 显示 和 隐藏 骰子容器 等
		*/
		private function displayUserPan(isDisplay = true):void {
			
			this.user_Instance.turnMe_mc.visible = isDisplay;
			this.user_Instance.paiArray_mc.visible = isDisplay;
			this.user_Instance.siceBg_mc.visible = isDisplay;
		}
		
		
		/*
		 * 显示 和 隐藏 其它用户的 牌 容器 等
		*/
		private function displayOtherControl(isDisplay = true):void {
			
			
			var testPlayer:* ;
			var relativePos:int;
			
			for (var i = 0; i < this.enter_length; i++) {
				
				if (this.enter_array[i].getPosition() != this.userPos) {
					
					relativePos = Tools.getPositionByPos( this.userPos, this.enter_array[i].getPosition() ); // 转换 为 相对位置
					
					testPlayer = this.gameAreaControl[ allInstance_array[relativePos] ]; //固定的位置
					testPlayer.paiArray_mc.sice08.gotoAndStop("isnull");
					testPlayer.paiArray_mc.visible = isDisplay;
					testPlayer.turnMe_mc.visible = isDisplay;
					
				}
				
			}
			
			
			
		}
		
		
		
		/*
		 * 隐藏 、显示  "准备" 对象
		*/
		private function displayUserChildren(isDisplay = false):void {
			
			
			var allInstance:*;
			for (var i = 0; i < 4; i++) {
				
				allInstance = this.gameAreaControl[ allInstance_array[i] ];
				if (i == 0) {
					
					this.users_timer.stopClock();
					this.users_timer.visible = isDisplay;
				}
				
				if (isDisplay == false) allInstance.ready_mc.gotoAndStop(3);
				
				allInstance.ready_mc.visible = isDisplay;
				
			}
			
			
			
			this.gameAreaControl.control_btns.trusteeship_btn.addEventListener(MouseEvent.CLICK, trusteeshipHandler); //托管 按钮
			
			
			//this.gameAreaControl.control_btns.full_btn.addEventListener(MouseEvent.CLICK, Main.getInstance().maxWinHandler );
			
		}
		private function maxWinHandler(event:MouseEvent):void {
			
			this._stageMain.stage.displayState = (this._stageMain.stage.displayState==StageDisplayState.NORMAL?StageDisplayState.FULL_SCREEN:StageDisplayState.NORMAL);
		}
		
		private var isClickTrusteeship:Boolean;
		private function trusteeshipHandler(event:MouseEvent):void {
			
			this.isClickTrusteeship = !this.isClickTrusteeship;
			
			if (this.isClickTrusteeship == true) {
				
				this.gameAreaControl.control_btns.trusteeship_btn.gotoAndStop(2);
				SoundControl.getInstance().loadSound("music/majDice/trusteeship.mp3");
				
				SendPackage.getInstance().trusteeship();//发包 托管
			}else {
				this.gameAreaControl.control_btns.trusteeship_btn.gotoAndStop(1);
				SoundControl.getInstance().loadSound("music/majDice/untrusteeship.mp3");
				
				SendPackage.getInstance().untrusteeship();//发包 解除托管
			}
			trace("是否托管 ： " + this.isClickTrusteeship);
			
		}
		private function soundHandler(event:MouseEvent):void {
			
			this.isOpenSnd = !this.isOpenSnd;
			if (this.isOpenSnd == true) {
				this.gameAreaControl.control_btns.sound_btn.gotoAndStop(2);
				
				SoundControl.getInstance().loadSound("music/majDice/soundBtn.mp3");//按钮声音
				
				SoundControl.getInstance().stopMusic();
			}else {
				
				SoundControl.getInstance().loadSound("music/majDice/soundBtn.mp3");//按钮声音
				
				this.gameAreaControl.control_btns.sound_btn.gotoAndStop(1);
				
				SoundControl.getInstance().playMusic();
			}
			trace("是否关闭声音 ： " + this.isOpenSnd);
			
			
		}
		
		
		
		
		
		
		
		
		
		/*
		 * 首发骰时的 摇动 动画 
		*/
		private function yaoSaiMove():void {
			
			var yaoSai_mc = this.gameAreaControl.yaoSaiMove_mc;
			yaoSai_mc.visible = true;
			yaoSai_mc.gotoAndPlay(2);
			
			yaoSai_mc.addFrameScript(0, function(){yaoSai_mc.stop()} , yaoSai_mc.totalFrames - 1, this.yaoSaiComplete); 
			
		}
		
		/*
		 * 摇骰 动画 播放完成
		*/
		private function yaoSaiComplete():void {
			
			var yaoSai_mc = this.gameAreaControl.yaoSaiMove_mc;
			this.gameAreaControl.allSize_mc.visible = true; //显示底牌影片剪辑
			yaoSai_mc.visible = false; //设置为false
			yaoSai_mc.addFrameScript(0, function() { yaoSai_mc.stop() } , yaoSai_mc.totalFrames - 1, null); 
				
			for (var i = 0; i < this.enter_length; i++) {
				
				trace("摇骰 动画 播放完成 this.enter_length == " + this.enter_length);
				trace("摇骰 动画 播放完成 keys == " + this.hashMap.keys()[i]);
				trace("摇骰 动画 播放完成 values == " + this.hashMap.values()[i]);
				
				this.changedUserPai( this.hashMap.keys()[i],this.hashMap.values()[i] ); //提取 牌
				
			}
			
			
			this.num_Instance = 0;
			
			this.faPaiMove();//发骰 动画
			
		}
		
		
		
		/*
		 * 出现骰 更新骰
		*/
		private function changedUserPai( pos:int , pai_array:Array , isvisible = false ):void {
			
			trace("pos = " + pos + "   " +"pai_array" + pai_array);
			
			var _thisPlayer = this.getUserByPos(pos);
			trace("Instance == " + _thisPlayer);
			trace("Instance name = " + _thisPlayer.name);
			
			var _array:Array = pai_array.sort();
			var _length:uint = pai_array.length;
			
			
			for (var i = 1; i <=8;i++ ) {
				var pai_mc:MovieClip = _thisPlayer.paiArray_mc["sice0" + i];
				//[1,3,3,3,3,4,5]
				if (isvisible == true) { //重连时用(isvisible == true)
					pai_mc.alpha = 1;
					pai_mc.visible = true;
				}
				
				pai_mc.gotoAndStop(_array[i-1]);
				
				if(i == 8){
					if (_length == 7 || isvisible == true ) {//重连时用(isvisible == true)
						
						pai_mc.gotoAndStop("isnull");
						
					}
				}
				
				
			}
			
			_thisPlayer.paiArray_mc.visible = true;
			
		}
		
		
		
		
		/*
		 * 发骰 动画 
		*/
		private function faPaiMove():void {
			
			this.tweenTogetherItems = new TweenTogetherItems(this.user_Instance.paiArray_mc, this.newX_array[ 0 ], this.newY_array[ 0 ], nextTween,dealWithPai);
			
		}
		private function nextTween():void {
			
			this.num_Instance++;
			trace("this.num_Instance = " + this.num_Instance);
			
			var testPlayer:* ;
			var relativePos:int =0;
			
			if (this.num_Instance  < 4 ) { //0,1,2,3  0是前面的本人
				
				for (var i = 0; i < this.enter_length; i++) {
					
					if (this.enter_array[i].getPosition() != this.userPos ) {
						
						relativePos = Tools.getPositionByPos( this.userPos, this.enter_array[i].getPosition() ); // 转换 为 相对位置
						
						if ( relativePos == this.num_Instance ) {
							break;
						}else {
							relativePos = 0;
						}
						
					}
					
				}
				
				
				if (relativePos != 0) {
					
					testPlayer = this.gameAreaControl[ allInstance_array[relativePos] ]; //固定的位置
					this.tweenTogetherItems = new TweenTogetherItems(testPlayer.paiArray_mc, this.newX_array[relativePos], this.newY_array[relativePos], nextTween,dealWithPai);
					
					//trace("本用户发骰完成");
					
				}else {
					
					nextTween();
				}
				
				
			}else {
				
				this.gameAreaControl.allSize_mc.visible = false; //隐藏 底牌 影片剪辑
				
				trace("::::::::::::::this.num_Instance::::::::::::::" + this.num_Instance + " this.enter_length-1 === " + (this.enter_length-1) )
				// 下面再还原它们的 visible 属性
				var size_mc;
				for (var j = 1; j < 5; j++) {
					
					size_mc = this.gameAreaControl.allSize_mc["mc" + j];
					size_mc.visible = true;
				}
				
				this.num_Instance = 0;
				
				//trace("发骰完成");
				//systemGetFirst(); //系统摇出第一张公共骰
				this.gameAreaControl.control_btns.trusteeship_btn.visible = true;// 这里才显示 托管按钮
				
			}
			
		}
		
		
		// 删除 中间 一块一块的 骰
		private function dealWithPai():void {
			
			var _mc = this.gameAreaControl.allSize_mc["mc" + (this.num_Instance+1)];
			_mc.visible = false;
			
			
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/*
		 * 显示打出去的骰点
		 */
		public function displayResulttSice(Num:uint, thisUser:* = null) {
			
			this.gameAreaControl.timer_mc.visible = false;//隐藏 倒计时(所有用户统一用这个)
			
			this.gameAreaControl.resulttSice.gotoAndStop(Num + 1);
			
			var newX:Number;
			var newY:Number;
			
			
			if (thisUser == null) {
				this.resulttSiceX = 80;
				this.resulttSiceY = -50;
				
				this.resulttSiceTween(this.resulttSiceX,this.resulttSiceY - 20);
				return;
			}
			
			
			switch (thisUser) {
				
				case this.gameAreaControl[ this.allInstance_array[0] ]:
					
					this.selectNum = 0;
					selectItemTween(true) //显示打出后,把牌的状态还原
					
					newX = -10;
					newY = 120;
					
					this.resulttSiceX = 10;
					this.resulttSiceY = 40;
					
					
					break;
				case this.gameAreaControl[ this.allInstance_array[1] ]:
					
					newX = 200;
					newY = -25;
					
					this.resulttSiceX = 95;
					this.resulttSiceY = -20;
					
					break;
				case this.gameAreaControl[ this.allInstance_array[2] ]:
					
					newX = 30;
					newY = -150;
					
					this.resulttSiceX = 10;
					this.resulttSiceY = -80;
					
					break;	
				case this.gameAreaControl[ this.allInstance_array[3] ]:
					
					newX = -200;
					newY = -25;
					
					this.resulttSiceX = -75;
					this.resulttSiceY = -20;
					
					break;		
			}
			
			this.resulttSiceTween(newX,newY);
			
		}
		
		private function resulttSiceTween(newX:Number, newY:Number) {
			
			this.gameAreaControl.resulttSice.visible = true;
			
			this.gameAreaControl.resulttSice.x = newX;
			this.gameAreaControl.resulttSice.y = newY;
			
			SoundControl.getInstance().loadSound("music/majDice/outSice.mp3");
			
			Tweener.addTween(this.gameAreaControl.resulttSice, {x:this.resulttSiceX,y:this.resulttSiceY, time:0.5});
		}
		
		
		/*
		 *  选择骰子 效果变化 （相当于骰子）
		*/
		private function selectItemTween(allTween:Boolean = false) {
			
			var pai_mc:MovieClip;
			for (var i = 1; i <= 8; i++ ) {
				
				pai_mc = this.gameAreaControl.player0.paiArray_mc["sice0" + i];
				
				if (allTween == true) {
					Tweener.addTween(pai_mc, {scaleX:1, scaleY:1, time:.3, transition:"easeInOutQuint"});
				}else {
					
					if (i == this.sicesPos) {
						Tweener.addTween(pai_mc, { scaleX:1.2, scaleY:1.2, time:.3, transition:"easeInOutQuint" } );
					
					}else {
						Tweener.addTween(pai_mc, {scaleX:1, scaleY:1, time:.3, transition:"easeInOutQuint"});
					}
					
				}
				
			}
			
		}
		
		
		
		
		/*
		 * 取骰 显示它
		*/
		private function getPlayersPai( _thisPlayer:* , num:uint ):void {
			
			var pai_mc:MovieClip = _thisPlayer.paiArray_mc.sice08;
			
			pai_mc.gotoAndStop( num );
			
			
		}
		
		/*
		 * 加入 一个数 
		*/
		private function joinPai( username:String, num:uint ):void {
			
			var new_array:Array = new Array();
			
			for (var i = 0; i < this.enter_length; i++) {
				
				if (this.enter_array[i].getLoginName() == username) {
					
					new_array = this.hashMap.getValue( this.enter_array[i].getPosition() );
					new_array.push(num);
					
					trace(" 取牌 " + this.hashMap.values());
					break;
					
				}
				
			}
			
			
		}
		
		/*
		 * 删除 一个数 
		*/
		private function splicePai( username:String, num:uint ):void {
			
			var new_array:Array = new Array();
			
			for (var i = 0; i < this.enter_length; i++) {
				
				if (this.enter_array[i].getLoginName() == username) {
					
					new_array = this.hashMap.getValue( this.enter_array[i].getPosition() );
					
					var findNum:int = new_array.indexOf(num);
					new_array.splice(findNum, 1); //删除
					
					trace(" 发牌 " + this.hashMap.values());
					break;
					
				}
				
			}
			
		}
		
		
		
		
		/*
		 * 重新 初始化游戏 
		*/
		public function reSet():void {
			
			this.hashMap.clear(); //清除
			
			this.isrest = true;
			
			this.isFirstPriority = false;
			
			this.initGame(this.gameAreaControl);
			
			
		}
		
		
		
		
		/*
		 * 更新信号
		*/
		public function updateSignal():void {
			
			if (SignalAction.getInstance() != null) {
				//trace("SignalAction.getInstance().recieveSignal();");
				SignalAction.getInstance().recieveSignal();
			}
			
		}
		
		
		/*
		 * 断线重连设置
		*/
		public function resetScene( userInfos:HashMap ):void {
			//隐藏中间的动画播放
			this._stageMain.middle_mc.sice_mc.gotoAndStop(42);
			this._stageMain.middle_mc.addFrameScript(0, function(){ }, this._stageMain.middle_mc.totalFrames - 1, this.hideMiddleText); 
			
			
			trace(" 断线重连设置: " + HashMap);
			GameAction.getInstance().resetConnectStatus();
		    var names:Array = userInfos.keys();
			var values:Array = userInfos.values();
			var _length:uint = names.length;
			
			var players:Array = new Array();
			for (var i = 0; i < _length; i++) {
				
				players.push(Players.getInstance().getPlayerInfo( names[i] ) );
			}
			GameAction.getInstance().enterGame( players ); //第一次进入游戏,初始化用户界面 enter_players
			
			GameAction.getInstance().typeOutControl( false ); //开始发牌  这里取消发牌的Tween动画
			
		    for (var j = 0; j < _length; j++) {
				
				GameAction.getInstance().resetInfo( players[j] , values[j] );
		    }
			
			
		}
		//隐藏中间的动画播放
		private function hideMiddleText():void {
			
			this._stageMain.middle_mc.stop();
			this._stageMain.middle_mc.start_txt.gotoAndStop(28);
		}
		
		
		
		/*
		 * 断线的人回来
		*/		
		public function userComeBack(player:Player):void {
			
			trace(" ************  断线的人回来  **********" + player.getLoginName() );
			
			ImgAction.getInstance().enterUser( player );
			GameAction.getInstance().outOfGame( player.getLoginName() ,false);
			
		}
		
		
		/*
		 * 断线回来设置最新状态
		*/
		public function resetNewestStatus(isPub:int,pos:int):void { //参数 pos 是 接力的位置  pos也需要转换this.enter_array[i].getPosition()
			
			var user:* = this.getUserByPos( pos );
			
			trace("断线回来设置最新状态 pos === " + pos );
			trace("断线回来设置最新状态 user.name === " + user.name );
			
			var dices:Array = this.hashMap.getValue(pos);
			
			if ( user != null ) {
				
				
				for (var i = 0; i < this.enter_array.length; i++ ) {
					
					if ( this.enter_array[i].getPosition() == pos ) {
						
						trace("断线回来设置最新状态 getLoginName() === " + this.enter_array[i].getLoginName() );
						GameAction.getInstance().priority( this.enter_array[i].getLoginName() );
						
						trace("断线回来设置最新状态 this.hashMap.values().length === " + this.hashMap.getValue(pos).length );
							
							
							
							
							
						if (this.hashMap.getValue(pos).length == 8) { 
							
							if ( this.enter_array[i].getPosition() == this.userPos) {//是不是本人
								
								
								this.changedUserPai(pos, dices);
								
								GameTip.writeOffInstance();// 注销一下
								var isHuPai:Boolean = GameTip.getInstance().setJoinPaiArray( dices );
								trace("提示是否 胡牌 ==== " + isHuPai);
								
								if (isHuPai) {//是否 胡牌
									
									this.displayUserControl(true);//隐藏 本人的一些控制按钮
									
									trace( "胡牌用户 ==== " + this.enter_array[i].getLoginName() );
									GameAction.getInstance().huPaiResults( this.enter_array[i].getLoginName() );//提示是否 胡牌
								}else {
									
									
									this.displayUserControl(true);//显示 本人的一些控制按钮
									
									sicesControlHandler();//统一 侦听每一个 骰子	
									this.user_Instance.getOut_btn.addEventListener(MouseEvent.CLICK, getOutItemHandler);
									this.user_Instance.tip_btn.addEventListener(MouseEvent.CLICK, getOutTipHandler);
								}
								
							}
							
							// 不管是不是本人，都得隐藏
							this.user_Instance.shak_mc.visible = false;//摇骰按钮
							this.user_Instance.shak_mc.mouseEnabled = false;
							
							
						}else {
							
							
							if (pos != this.userPos) {
								
								this.user_Instance.shak_mc.visible = false;//摇骰按钮
								this.user_Instance.shak_mc.mouseEnabled = false;
							}else {
								
								if (isPub == 1) {
									
									this.user_Instance.shak_mc.visible = false;//摇骰按钮
									this.user_Instance.shak_mc.mouseEnabled = false;
									
									this.gameAreaControl.resulttSice.visible = true;
									
								}
								
								
							}
							
							
						}
						
						
						
						
						
						
						
						
					}
					
				}
				
				
				
				
			}
			
			
		}
		
		
		/*
		 * 重设重连回来的初始状态
		*/
		public function resetConnectStatus():void {
			
			//clearTimer();
			this.users_timer.stopClock();
			this.users_timer.visible = false;
			
			this.user_Instance.ready_mc.gotoAndStop(3);
		}
		
		/*
		 * 重构游戏界面
		*/
		public function resetInfo( player:Player, info:Array ):void {
			
			
			var pai_array:Array = info[0];
			var status:int = info[1];
			var trusteeship:Boolean = info[2];
			
			//this.gameStart( player.getPosition() , pai_array );// 首发牌给各个玩家
			this.hashMap.put( player.getPosition() , pai_array.sort() );
			
			
			
			
			
			//trace(info[1] + "++++++++++++++++++++++++");
			
			if (trusteeship) GameAction.getInstance().trusteeship( player.getLoginName() ,true);
			this.changedUserPai( player.getPosition() , pai_array , true );//出现牌 更新牌   这里直接更新 并设置显示
			
			
			var username:String = player.getLoginName();
			if (player.getLoginName() == User.getInstance().getLoginName()) {//my
				
				
				this.gameAreaControl.control_btns.trusteeship_btn.gotoAndStop(1);
				this.gameAreaControl.control_btns.trusteeship_btn.visible = true;//显示 托管等按钮
				
				
				
				if (status == 0) { //0.打骰中  1.出骰中 2.胡牌以后的状态
					
					//GameAction.getInstance().priority( username ); //接力 倒计时和添加事件侦听
					
				}else if (status == 1) {
					
					//GameAction.getInstance().displayOutPai( username , pai_array[7] ); //收到 摇骰后的牌
					
				}else if (status == 2) {
					
					//this.changedUserPai( player.getPosition() , pai_array );//出现牌 直接显示
				}
				
				
				
			}else {
				
				//var userPos:int = Tools.getPositionByPos(this.userPos, player.getPosition());
				
				if (status == 0) { //0.打骰中  1.出骰中 2.胡牌以后的状态
					
					//GameAction.getInstance().priority( username ); //接力 倒计时
					//GameAction.getInstance().userYaoSai( username ); //摇骰动画播放
					
				}else if (status == 1) {
					
					//GameAction.getInstance().displayOutPai( username , pai_array[7] ); //收到 摇骰后的牌
					
				}else if (status == 2) {
					
					//this.changedUserPai( player.getPosition() , pai_array );//出现牌 直接显示
				}
				
			}
			
			
			
			
		}
		
		
		
		/*
		 * 更新某一用户列表
		*/
		public function updateUser(player:Player):void {
			
			this.infoPanel.playerList.refreshText( player );
			
			if( player.getLoginName() == User.getInstance().getLoginName() ) this.infoPanel.userInfo.refreshText(); //更新本人信息块
			
		}
		
		
		
		/*
		 * 把不必要的引用设为null
		*/
		public function setStaticObjectNull():void {
			
			if ( ImgAction.getInstance() ) ImgAction.getInstance().setStaticObjectNull();
			if ( SendPackage.getInstance() ) SendPackage.getInstance().setStaticObjectNull();
			if ( SoundControl.getInstance() ) SoundControl.getInstance().setStaticObjectNull();
			if ( QcontextMenu.getInstance() ) QcontextMenu.getInstance().setStaticObjectNull();
			if ( LoaderImg.getInstance() ) LoaderImg.getInstance().setStaticObjectNull();
			if ( SignalAction.getInstance() ) SignalAction.getInstance().setStaticObjectNull();
			
			if ( ToolTip.getInstance() ) {
				
				ToolTip.getInstance().hideToolTip(); //清除tip文本
				ToolTip.getInstance().setStaticObjectNull();
				
			}
			
			Instance = null;
		}
		
		
		
		
	}
	
}

